﻿Imports EntityVisualizers35
Imports EVTest35.AWModel
Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.ComponentModel
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses

Public Class Form1

    Private _context As AdventureWorksLT2008R2Entities

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        _context = New AdventureWorksLT2008R2Entities()

        Dim products As ObjectResult(Of Product) = _
            _context.Products _
            .Execute(Objects.MergeOption.AppendOnly)

        Me.ProductBindingSource.DataSource = products
    End Sub

    Private Sub ButtonContext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonContext.Click
        ' load another entity for testing
        _context.AddToCustomers(New Customer())
        Dim customers As IList = _context.Customers.ToList()

        Dim visualizerHost As New VisualizerDevelopmentHost(_context, _
                                                            GetType(ObjectContextVisualizer), _
                                                            GetType(ObjectContextVisualizerObjectSource))
        visualizerHost.ShowVisualizer()
    End Sub

    Private Sub btnQuery_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnQuery.Click
        Dim objectQuery As ObjectQuery = _context.Products ' From x In _context.Products Where x.ProductID = 999 Select x
        Dim visualizerHost As New VisualizerDevelopmentHost(objectQuery, _
                                                            GetType(ObjectQueryVisualizer), _
                                                            GetType(ObjectQueryVisualizerObjectSource))
        visualizerHost.ShowVisualizer()
    End Sub

    Private Sub btnEntityCollection_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEntityCollection.Click
        Dim load = _context.Products.Include("SalesOrderDetails").ToList()

        Dim coll = (From p In _context.Products _
                   Where p.SalesOrderDetails.Count > 0 _
                   Select p).First.SalesOrderDetails

        coll.Add(New SalesOrderDetail()) ' sample added entity

        '' alternative test code for a collection that is detached, generates error message
        'Dim testCustomer = New Customer With {.CustomerID = 999, .LastName = "testCustomer"}
        '_context.AddToCustomers(testCustomer)
        '_context.AddToSalesOrderHeaders(New SalesOrderHeader With {.Customer = testCustomer})
        'Dim coll = testCustomer.SalesOrderHeaders

        Dim visualizerHost As New VisualizerDevelopmentHost(coll, _
                                                            GetType(EntityCollectionVisualizer), _
                                                            GetType(EntityCollectionVisualizerObjectSource))
        visualizerHost.ShowVisualizer()
    End Sub

    Private Sub btnEntity_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEntity.Click
        If (ProductDataGridView.SelectedRows.Count = 0) Then
            MessageBox.Show("Please select a row")
            Return
        End If
        Dim selectedEntity As Object = ProductDataGridView.SelectedRows(0).DataBoundItem
        Dim visualizerHost As New VisualizerDevelopmentHost(selectedEntity, _
                                                            GetType(EntityVisualizer), _
                                                            GetType(EntityVisualizerObjectSource))
        visualizerHost.ShowVisualizer()
    End Sub

    Private _adding As Boolean = False

    Private Sub ProductBindingSource_AddingNew(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles ProductBindingSource.AddingNew
        _adding = True
    End Sub

    Private Sub ProductBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ProductBindingSource.CurrentChanged
        If (Not _adding) Then Return
        _adding = False
        ProductBindingSource.EndEdit()
    End Sub

End Class
